package com.zwj.controller;

import com.zwj.entity.Student;
import com.zwj.service.IClassesService;
import com.zwj.service.IStudentService;
import com.zwj.util.PageResult;
import com.zwj.util.R;
import com.zwj.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/student")
public class StudentController {
    @Autowired
    private IStudentService studentService;

    @Autowired
    private IClassesService classesService;

    @RequestMapping("/list")
    public R getStudent() {
        return R.ok().data("list", studentService.list());
    }

    @GetMapping("/findByPage/{page}/{pageSize}")
    public R findByPage(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize) {
        PageResult<Student> pr = studentService.findByPage(page, pageSize);
        return R.ok().data("pr", pr);
    }

    @PostMapping("/search/{page}/{pageSize}")
    public R search(@PathVariable("page") Integer page, @PathVariable("pageSize") Integer pageSize,
                    @RequestBody(required = false) StudentVo studentVo) {
        PageResult<Student> pr = studentService.search(page, pageSize, studentVo);
        return R.ok().data("pr", pr);
    }

    @PostMapping("/save")
    public R add(@RequestBody Student student) {
        student.setCname(classesService.getById(student.getCid()).getCname());
        student.setDeleted(0);
        return studentService.save(student) ? R.ok().message("添加成功喵!") : R.error().message("添加失败，嘻嘻！");
    }

    @PutMapping("/update")
    public R update(@RequestBody Student student) {
        student.setDeleted(0);
        return studentService.updateById(student) ? R.ok().message("修改成功！") : R.ok().message("修改失败！");
    }

    @DeleteMapping("/delete/{id}")
    public R delete(@PathVariable int id){
       return studentService.removeById(id) ? R.ok().message("删除成功！") : R.ok().message("删除失败！");
    }

}
